﻿//1.全局变量

//2.初始化界面展示方法
function fn_deptLayerTree() {
    $('#deptLayerTree').tree({
        url: "Controller.aspx?controller=DeptEmployeeController&method=GetDeptLayers",
        onDblClick: function(node) {
            $('#deptLayerTree').tree('beginEdit', node.target);
        },
        onClick: function(node) {
            var children = $('#deptLayerTree').tree('getChildren', node.target);
            var s = node.id;
            for (var i = 0; i < children.length; i++) {
                if (s != '') s += ',';
                s += children[i].id;
            }
            $('#deptGrid').datagrid('options').url = 'Controller.aspx?controller=DeptEmployeeController&method=GetDeptList&layerId=' + s;
            $('#deptGrid').datagrid('reload');
        },
        onAfterEdit: function(node) {
            var parent = $('#deptLayerTree').tree('getParent', node.target);
            var data = { layerId: node.id, Name: node.text, PId: parent ? parent.id : 0 };
            requestAjax("Controller.aspx?controller=DeptEmployeeController&method=AlterDeptLayer", data);
        }
    });
}
function alterdept_handler() {
    $('#dialog-dept').dialog({ title: '修改部门' });
    var selected = $('#deptGrid').datagrid('getSelected');
    if (selected) {
        $("#deptform").form("load", selected);

        if (selected.DeleteBit == "1")
            $("#_DeleteBit").attr("checked", "true");
        else
            $("#_DeleteBit").removeAttr("checked"); 
        $("#DeleteBit").val(selected.DeleteBit);

        $('#dialog-dept').dialog('open');
    }
}
function fn_deptGrid() {
    $('#deptGrid').datagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [{
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                var SelectedNode = $('#deptLayerTree').tree('getSelected');
                if (SelectedNode) {
                    $('#dialog-dept').dialog({ title: '添加部门' });
                    $('#dialog-dept').dialog('open');
                    $("#deptform").form('clear');
                    $("#PDeptId").val(SelectedNode.id);
                    $("#DeptId").val(0);
                    $("#DeleteBit").val(0);
                }
            }
        }, {
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
                alterdept_handler();
            }
}],
            onDblClickRow: function() {
                alterdept_handler();
            }
        });
    }
function alteremployee_handler() {
    $('#dialog-employee').dialog({ title: '修改人员' });
    var selected = $('#employeeGrid').datagrid('getSelected');
    if (selected) {
        //$("#employeeform").form("load", selected);
        $("#employee-id").val(selected.EMPLOYEE_ID);
        $("#user-id").val(selected.USER_ID);
        $("#employee-name").val(selected.NAME);
        $("#employee-sex").combobox('setValue', selected.SEX);
        
        $("#user-code").val(selected.CODE);


        if (selected.DELETE_BIT == "1")
            $("#employee-delete").attr("checked", "true");
        else
            $("#employee-delete").removeAttr("checked");
        if (selected.LOCK == "1")
            $("#user-lock").attr("checked", "true");
        else
            $("#user-lock").removeAttr("checked");
        $("#employeedelete").val(selected.DELETE_BIT);
        $("#userlock").val(selected.LOCK);
        
        $('#dialog-employee').dialog('open');
        //employee - depts
        requestAjax('Controller.aspx?controller=DeptEmployeeController&method=GetDeptEmployee', { empId: selected.EMPLOYEE_ID }, function(data) {
            $("#employee-depts").combotree('setValues', data);
        });
        //user - groups
        //requestAjax('Controller.aspx?controller=DeptEmployeeController&method=GetGroupUser', { userId: selected.USER_ID }, function(data) {
            //$("#user-groups").combobox('setValues', data);
        //});
    }
 }
function fn_employeeGrid() {
    $('#employeeGrid').datagrid({
        fit: true,
        fitColumns: true,
        rownumbers: true,
        singleSelect: true,
        toolbar: [{
            id: 'btnsearch-detail',
            text: '查询',
            iconCls: 'icon-search',
            handler: function() {
                $('#dialog-searchEmployee').dialog({ title: '查询人员' });
            }
        }, {
            id: 'btnreload-detail',
            text: '刷新',
            iconCls: 'icon-reload',
            handler: function() {
                $('#employeeGrid').datagrid('reload');
            }
        }, {
            id: 'btnadd-detail',
            text: '添加',
            iconCls: 'icon-add',
            handler: function() {
                $('#dialog-employee').dialog({ title: '添加人员' });
                $('#dialog-employee').dialog('open');
                $("#employeeform").form('clear');
                $("#employee-sex").combobox('setValue', -1);
                $("#employee-id").val(0);
                $("#user-id").val(0);
                $("#employeedelete").val(0);
                $("#userlock").val(0);
            }
        }, {
            id: 'btnedit-detail',
            text: '修改',
            iconCls: 'icon-edit',
            handler: function() {
                alteremployee_handler();
            }
        }, {
            id: 'btnpassword-detail',
            text: '重置密码',
            iconCls: 'icon-edit',
            handler: function() {
                var selected = $('#employeeGrid').datagrid('getSelected');
                if (selected) {
                    $.messager.confirm('询问', '是否需要重置密码?', function(r) {
                        if (r) {
                            requestAjax('Controller.aspx?controller=DeptEmployeeController&method=ResetPassword', { userId: selected.USER_ID });
                        }
                    });
                }
            }
}],
            onDblClickRow: function() {
                alteremployee_handler();
            }
        });
    }

function fn_dialog_deptlayer() {
    //新增、修改部门类别弹出框
    $('#dialog-deptlayer').dialog({
        modal: true,
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function() {
                FormSubmit($('#deptlayerform'), 'Controller.aspx?controller=DeptEmployeeController&method=AddDeptLayer', function() {
                    $('#dialog-deptlayer').dialog('close');
                    $('#deptLayerTree').tree('reload');
                });
            }
        }, {
            text: '取消',
            handler: function() {
                $('#dialog-deptlayer').dialog('close');
            }
}]
        });

        $('#dialog-deptlayer').dialog('close');
    }

    function fn_dialog_dept() {
        //新增、修改部门弹出框
        $('#dialog-dept').dialog({
            modal: true,
            buttons: [{
                text: '确定',
                iconCls: 'icon-ok',
                handler: function() {
                FormSubmit($('#deptform'), 'Controller.aspx?controller=DeptEmployeeController&method=SaveDept', function() {
                        $('#dialog-dept').dialog('close');
                    });
                }
            }, {
                text: '取消',
                handler: function() {
                    $('#dialog-dept').dialog('close');
                }
}]
            });

            $('#dialog-dept').dialog('close');
        }
        
        function fn_dialog_searchEmployee() {
            //查询人员弹出框
            $('#dialog-searchEmployee').dialog({
                modal: true,
                buttons: [{
                    text: '确定',
                    iconCls: 'icon-ok',
                    handler: function() {
                        var deptid = $('#sdeptId').combobox('getValues');
                        //var groupid = $('#sgroupId').combobox('getValue');
                        var empname = $('#sempName').val();
                        $('#employeeGrid').datagrid('options').url = 'Controller.aspx?controller=DeptEmployeeController&method=GetEmployeeUserList&deptId=' + deptid.join(',') + '&empName=' + empname;
                        $('#employeeGrid').datagrid('reload');
                        $('#dialog-searchEmployee').dialog('close');
                    }
                }, {
                    text: '取消',
                    handler: function() {
                        $('#dialog-searchEmployee').dialog('close');
                    }
}]
                });

                $('#dialog-searchEmployee').dialog('close');
            }

            function fn_dialog_employee() {
                //查询人员弹出框
                $('#dialog-employee').dialog({
                    modal: true,
                    buttons: [{
                        text: '确定',
                        iconCls: 'icon-ok',
                        handler: function() {
                            FormSubmit($('#employeeform'), 'Controller.aspx?controller=DeptEmployeeController&method=SaveEmployeeUser', function() {
                                $('#dialog-employee').dialog('close');
                                var deptid = '';
                                //var groupid = -1;
                                var empname = $('#user-code').val();
                                $('#employeeGrid').datagrid('options').url = 'Controller.aspx?controller=DeptEmployeeController&method=GetEmployeeUserList&deptId=' + deptid + '&empName=' + empname;
                                $('#employeeGrid').datagrid('reload');
                            });
                        }
                    }, {
                        text: '取消',
                        handler: function() {
                            $('#dialog-employee').dialog('close');
                        }
}]
                    });

                    $('#dialog-employee').dialog('close');
                }

                function fn_InitCheckBox() {
                    $("#employee-delete").click(function() {
                        if ($("#employee-delete").attr("checked")) {
                            $("#employeedelete").val(1);
                        }
                        else {
                            $("#employeedelete").val(0);
                        }
                    });
                    $("#user-lock").click(function() {
                        if ($("#user-lock").attr("checked")) {
                            $("#userlock").val(1);
                        }
                        else {
                            $("#userlock").val(0);
                        }
                    });
                    $("#_DeleteBit").click(function() {
                        if ($("#_DeleteBit").attr("checked")) {
                            $("#DeleteBit").val(1);
                        }
                        else {
                            $("#DeleteBit").val(0);
                        }
                    });
                }
//3.初始化入口
                $(function() {

                    fn_deptGrid();
                    fn_employeeGrid();
                    fn_dialog_deptlayer();
                    fn_dialog_dept();
                    fn_dialog_searchEmployee();
                    fn_dialog_employee();
                    fn_deptLayerTree();
                    fn_InitCheckBox();
                });

//4.页面方法

    function deptlayer_refresh() {
        $('#deptLayerTree').tree('reload');
    }

    function deptlayer_append() {
        var SelectedNode = $('#deptLayerTree').tree('getSelected');
        if (SelectedNode) {
            $('#dialog-deptlayer').dialog({ title: '添加部门分类' });
            $('#dialog-deptlayer').dialog('open');
            $("#deptlayerform").form('clear');
            $("#PId").val(SelectedNode.id);
            $("#PName").val(SelectedNode.text);
        }
    }
    
    function deptlayer_delete() {
        var SelectedNode = $('#deptLayerTree').tree('getSelected');
        if (SelectedNode) {
            if (!confirm("确定要删除吗？"))
                return false;
            var data = { deptLayerId: SelectedNode.id };
            requestAjax("Controller.aspx?controller=DeptEmployeeController&method=DeleteDeptLayer", data, function(retobject) {
                $('#deptLayerTree').tree('reload');
            });
        }
    }
    

                       